package com.tramy.data.export.engine.impl;

import com.tramy.data.export.datasource.DatabaseContextHolder;
import com.tramy.data.export.datasource.DatabaseType;
import com.tramy.data.export.engine.DataEngine;
import com.tramy.data.export.mapper.CupSettlementMapper;
import com.tramy.data.export.mapper.SettlementCustomerMapper;
import com.tramy.data.export.model.CupSettlement;
import com.tramy.data.export.model.CupSettlementExample;
import com.tramy.data.export.model.SettlementCustomer;
import com.tramy.data.export.model.SettlementCustomerExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by honway on 2016/11/3.
 */
@Service
public class CheckoutCustomerDataEngine implements DataEngine {

    @Autowired
    private SettlementCustomerMapper settlementCustomerMapper;
    @Autowired
    private CupSettlementMapper cupSettlementMapper;


    @Override
    public void justDoIt() {
        //连接新烽火(结账客户数据)
        DatabaseContextHolder.setDatabaseType(DatabaseType.firehome);
        //查询新烽火库里所有的结账客户
        SettlementCustomerExample settlementCustomerExample = new SettlementCustomerExample();
        settlementCustomerExample.createCriteria().andCustomerCodeIsNotNull();
        System.out.println("开始从新烽火查询结账客户数据");
        List<SettlementCustomer> settlementCustomerList = settlementCustomerMapper.selectByExample(settlementCustomerExample);
        System.out.println("从新烽火中查询到:"+settlementCustomerList.size()+"条数据.");
        DatabaseContextHolder.setDatabaseType(DatabaseType.pinshang);
        System.out.println("开始将数据插入到新清美");

        if(settlementCustomerList.size()!= 0){
            //循环迭代结账客户
            CupSettlement cupSettlement = new CupSettlement();
            for (SettlementCustomer settlementCustomer : settlementCustomerList) {
                CupSettlementExample cupSettlementExample = new CupSettlementExample();
                cupSettlementExample.createCriteria().andCustomerCodeEqualTo(settlementCustomer.getCustomerCode()).
                        andCustomerNameEqualTo(settlementCustomer.getCustomerName());
                //System.out.println("条件查询数据"+settlementCustomer.getCustomerCode()+","+settlementCustomer.getCustomerName());
                List<CupSettlement> cupSettlements = cupSettlementMapper.selectByExample(cupSettlementExample);
                if(cupSettlements.size() == 0){
                    CupSettlement cup = cupSettlement.convert(settlementCustomer);
                    //System.out.println("convert result:"+JSON.toJSONString(cup));
                    cupSettlementMapper.insertSelective(cup);
                }
            }
        }

    }
}
